//
//  BeautyCollectionViewController.m
//  Car
//
//  Created by 何晓文 on 15/9/22.
//  Copyright © 2015年 朱鸿儒. All rights reserved.
//

#import "BeautyCollectionViewController.h"
#import "BeautyModel.h"
#import "BlurImageView.h"
#import "FormCollectionViewCell.h"
#import "RoyceLineLayout.h"
#import "FlowCollectionViewCell.h"
#import "UMSocial.h"
#import "MJRefresh.h"
#import "BeautyDetailViewController.h"

#define kFormUrl @"http://app.api.autohome.com.cn/autov4.8.8/club/jingxuantopic-pm1-c104-p%d-s30.json"
@interface BeautyCollectionViewController ()<UICollectionViewDelegateFlowLayout,UICollectionViewDataSource,UICollectionViewDelegate,UIWebViewDelegate>

@property (nonatomic, strong) BlurImageView *blur;
@property (nonatomic, strong) NSMutableArray *formList;
@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) NSMutableArray  *webArray;
@property (nonatomic, assign) int page;
@end

@implementation BeautyCollectionViewController

static NSString * const reuseIdentifierLine = @"Line";
static NSString * const reuseIdentifierFlow = @"Flow";

- (NSMutableArray *)formList {
	if (!_formList) {
		_formList = [[NSMutableArray alloc] init];
	}
	
	return _formList;
}
- (NSMutableArray *)webArray {
	if (!_webArray) {
		_webArray = [[NSMutableArray alloc] init];
	}
	return _webArray;
}

#pragma mark <-<-<-<-------解析论坛数据------->->->->
- (void)initData {
	[LORequestManger GET:[NSString stringWithFormat:kFormUrl,self.page] success:^(id response) {
		NSDictionary * dict = (NSDictionary *)response;
		NSDictionary * list = dict[@"result"][@"list"];
		for (NSDictionary *dictionary in list) {
			BeautyModel *model = [BeautyModel objectWithKeyValues:dictionary];
			[self.formList addObject:model];
		}
		[self.collectionView reloadData];
		[ProgressHUD showSuccess:nil];
	} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
		NSLog(@"%@", error);
		[ProgressHUD showError:@"网络错误"];
	}];
	
}
- (void)viewWillAppear:(BOOL)animated {
	[super viewWillAppear:YES];
	self.tabBarController.tabBar.hidden = YES;

}

- (void)viewDidLoad {
    [super viewDidLoad];
	self.page = 1;
	[ProgressHUD show:nil];
	[self initData];
	
	self.view.userInteractionEnabled = YES;

	self.collectionView = [[UICollectionView alloc]initWithFrame:kMainScreen collectionViewLayout:[[RoyceLineLayout alloc] init]];
	//背景模糊视图
	self.blur = [[BlurImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
	self.blur.alpha= 1;
	self.collectionView.backgroundView = self.blur;
	self.collectionView.delegate = self;
	self.collectionView.dataSource =self;
	[self.view addSubview:self.collectionView];
    [self.collectionView registerClass:[FormCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifierLine];
	
	[self.collectionView registerClass:[FlowCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifierFlow];
	/**
	 切换布局按钮
	 */
	UIBarButtonItem *switchLayout = [[UIBarButtonItem alloc] initWithTitle:@"摇一摇切换布局" style:UIBarButtonItemStyleDone target:self action:@selector(switchLayout)];
									
	self.navigationItem.rightBarButtonItem = switchLayout;
 /**
  *  下拉加载
  */
	__block BeautyCollectionViewController *mark = self;
	__block int a = 1;
	[self.collectionView addFooterWithCallback:^{
		a++;
		mark.page = a;
		[mark initData];
		[mark.collectionView footerEndRefreshing];
	}];


}

#pragma mark <-<-<-<-------切换布局------->->->->
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {

	[self switchLayout];
}
- (void)switchLayout {
	/**
	 *  横向布局
	 */
	if ([self.collectionView.collectionViewLayout isKindOfClass:[RoyceLineLayout class]]) {
		[self.collectionView setCollectionViewLayout:[[UICollectionViewFlowLayout alloc]init] animated:YES];
		[self.collectionView reloadData];
		CGFloat currentY =  self.collectionView.contentOffset.y;
		self.collectionView.contentOffset = CGPointMake(self.collectionView.contentOffset.x,currentY + 500);
		
	}else if
	/**
	 *  普通流水布局
	 */
	([self.collectionView.collectionViewLayout isKindOfClass:[UICollectionViewFlowLayout class]]) {
	[self.collectionView setCollectionViewLayout:[[RoyceLineLayout alloc]init] animated:YES];
		[self.collectionView reloadData];
		CGFloat currentX =  self.collectionView.contentOffset.x;
		self.collectionView.contentOffset = CGPointMake(currentX + kWidth *2, self.collectionView.y);
	}

}
#pragma mark <UICollectionViewDataSource>

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.formList.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
	/**
	 *  横向布局
	 */
	if ([self.collectionView.collectionViewLayout isKindOfClass:[RoyceLineLayout class]]) {
		FormCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifierLine forIndexPath:indexPath];
		cell.backgroundColor = [UIColor whiteColor];
		cell.beauty = self.formList[indexPath.row];
		return cell;
	}
	/**
	 *  流水布局else if
	 */

			FlowCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifierFlow forIndexPath:indexPath];
			cell.backgroundColor = [UIColor whiteColor];
			cell.beauty = self.formList[indexPath.row];
			return cell;

}

#pragma mark <UICollectionViewDelegate>
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
	/**
	 *  横向布局
	 */
	if ([self.collectionView.collectionViewLayout isKindOfClass:[RoyceLineLayout class]]) {
		return CGSizeMake(kWidth* 0.45, kHeight * 0.4);
		//切换cell
	}else if
	/**
	 *  普通流水布局
	 */
		([self.collectionView.collectionViewLayout isKindOfClass:[UICollectionViewFlowLayout class]]) {
			return CGSizeMake(kWidth* 0.45, 180 * 0.6);
		}
	return CGSizeMake(kWidth* 0.45, kHeight * 0.4);

}

//判断哪个cell显示改变模糊视图的图片
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {

	if ([self.collectionView.collectionViewLayout isKindOfClass:[RoyceLineLayout class]]) {
		if (self.collectionView.contentOffset.x > (self.formList.count - self.page) * 175) {
			self.page ++;
			[self initData];
			[ProgressHUD show:@"正在加载"];
		}
	}
	
	
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
	return UIEdgeInsetsMake(20, 10, 20, 10);

}
#pragma mark <-<-<-<-------item点击方法------->->->->
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
	BeautyModel *model = self.formList[indexPath.row];

	BeautyDetailViewController *detail = [[BeautyDetailViewController alloc] init];
	detail.webUrl = [NSString stringWithFormat:kShowURL,model.topicid];
	detail.model = model;
	[self presentViewController:detail animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning];
	[[SDWebImageManager sharedManager] cancelAll];
	[[SDWebImageManager sharedManager].imageCache clearMemory];
}

- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:YES];
	
}

@end
